package com.jmuok.web.fff.web.ui;

import org.springframework.context.i18n.LocaleContextHolder;
import org.vaadin.addon.customfield.beanfield.BeanFieldWrapper;
import org.vaadin.addon.customfield.beanfield.BeanSetFieldWrapper;

import com.jmuok.web.fff.domain.Comment;
import com.jmuok.web.fff.domain.Foodorder;
import com.jmuok.web.fff.domain.MenuUser;
import com.jmuok.web.fff.domain.Payment;
import com.jmuok.web.fff.domain.Role;
import com.jmuok.web.fff.domain.Schedule;
import com.jmuok.web.fff.domain.Usergroup;
import com.jmuok.web.fff.util.ApplicationHelper;
import com.vaadin.data.Item;
import com.vaadin.data.validator.EmailValidator;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DateField;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.FormFieldFactory;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.TwinColSelect;

public class UserRegForm extends UserForm {
	//private static Logger logger = Logger.getLogger(UserRegForm.class);
	private static final long serialVersionUID = -1690336647883920214L;
	private ApplicationHelper helper = ApplicationHelper.instance();

	private String[] fieldsToShow = new String[] { "userName", "fullName",
			"password", "email", "phoneNumber", "usergroupId", "roleId" };

	public UserRegForm() {
		setCaption(helper.getMessage("signup.heading"));
		setDeleteAllowed(false);

	}

	@Override
	public void setItemDataSource(Item item) {
		super.setItemDataSource(item);
		getForm().setVisibleItemProperties(fieldsToShow);
	}

	public FormFieldFactory getFormFieldFactory() {
		return new DefaultFieldFactory() {
			@Override
			public Field createField(Item item, Object propertyId,
					Component uiContext) {
				Field field = null;
				if (getIdProperty().equals(propertyId)
						|| getVersionProperty().equals(propertyId)) {
					return null;
				} else if ("schedules".equals(propertyId)) {
					TwinColSelect select = buildSchedulesMultiSelect();
					field = new BeanSetFieldWrapper<Schedule>(select,
							Schedule.class, getContainerForSchedules(), "id");
					field.setCaption(createCaptionByPropertyId(propertyId));
				} else if ("foodorders".equals(propertyId)) {
					TwinColSelect select = buildFoodordersMultiSelect();
					field = new BeanSetFieldWrapper<Foodorder>(select,
							Foodorder.class, getContainerForFoodorders(), "id");
					field.setCaption(createCaptionByPropertyId(propertyId));
				} else if ("usergroupId".equals(propertyId)) {
					ComboBox combo = buildUsergroupIdCombo();
					field = new BeanFieldWrapper<Usergroup>(combo,
							Usergroup.class, getContainerForUsergroups(), "id");
					field.setCaption(createCaptionByPropertyId(propertyId));
				} else if ("payments".equals(propertyId)) {
					TwinColSelect select = buildPaymentsMultiSelect();
					field = new BeanSetFieldWrapper<Payment>(select,
							Payment.class, getContainerForPayments(), "id");
					field.setCaption(createCaptionByPropertyId(propertyId));
				} else if ("menuUsers".equals(propertyId)) {
					TwinColSelect select = buildMenuUsersMultiSelect();
					field = new BeanSetFieldWrapper<MenuUser>(select,
							MenuUser.class, getContainerForMenuUsers(), "id");
					field.setCaption(createCaptionByPropertyId(propertyId));
				} else if ("comments".equals(propertyId)) {
					TwinColSelect select = buildCommentsMultiSelect();
					field = new BeanSetFieldWrapper<Comment>(select,
							Comment.class, getContainerForComments(), "id");
					field.setCaption(createCaptionByPropertyId(propertyId));
				} else if ("roleId".equals(propertyId)) {
					ComboBox combo = buildRoleIdCombo();
					field = new BeanFieldWrapper<Role>(combo, Role.class,
							getContainerForRoles(), "id");
					field.setCaption(createCaptionByPropertyId(propertyId));
				} else if ("password".equals(propertyId)) {
					field = new PasswordField();
					field.setWidth("181px");
					field.setCaption(createCaptionByPropertyId(propertyId));
					((PasswordField) field).setNullRepresentation("");
					field.setRequired(true);
				} else {
					field = super.createField(item, propertyId, uiContext);
					if (field instanceof TextField) {
						((TextField) field).setNullRepresentation("");
						field.setWidth("181px");
					}
					if (field instanceof DateField) {
						((DateField) field).setLocale(LocaleContextHolder
								.getLocale());
						field.setInvalidAllowed(false);
					}
				}

				if ("email".equals(propertyId)) {
					String value = "";
					field.addValidator(new EmailValidator(helper.getMessage(
							"errors.email", (String) value)));

				}

				return field;
			}
		};
	}



//	@Transactional
//	private boolean doCommit() {
//		try {
//			this.commit();
//			User entity = getEntityForItem(getItemDataSource());
//			JPAContainer<User> container = getContainer();
//			container.setEntityProvider(EntityProviderUtil.get()
//					.getEntityProvider(User.class));
//			container.addEntity(entity);
//			return true;
//		} catch (InvalidValueException e) {
//			// show validation error also on the save button
//			setCommitErrorMessage(e.getMessage());
//			return false;
//		}
//	}

//	public User getEntityForItem(Item item) {
//		if (item instanceof EntityItem
//				&& ((EntityItem) item).getEntity() instanceof User) {
//			return (User) ((EntityItem) item).getEntity();
//		} else {
//			return null;
//		}
//	}
//
//	private JPAContainer<User> getContainer() {
//		if (container == null) {
//			container = new JPAContainer<User>(User.class);
//			container.setEntityProvider(EntityProviderUtil.get()
//					.getEntityProvider(User.class));
//		}
//		return container;
//	}
}
